<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/3/16 0016
 * Time: 16:06
 */
namespace app\admin\controller;
use app\models\Models;

class GoodsType extends Check{
    /**
     * 商品类型列表
     * @return \think\response\View
     */
    public function typeList(){
        $where = [];
        // 判断商家登录
        $group_arr = self::getGroup();
        if($group_arr['group_id'] == Models::GROUP_BUSINESS){
            $flag = true;
            $where = ['a.bid'=>[['eq',0],['eq',$group_arr['id']],'or']];
        }
        $type = new Models('goodsType');
        $list = $type->joinMany([['business b','a.bid = b.id','left']],$where,'a.*,b.name business');
        return view('typeList',['list'=>$list,'flag'=>$flag]);
    }

    /**
     * 商品类型添加
     * @return array|\think\response\View
     */
    public function typeAdd(){
        // 判断商家登录
        $group_arr = self::getGroup();
        if(request()->isAjax()){
            $post = input('param.');
            $type = new Models('goodsType');
            try{
                // 添加场景验证
                $validate = $this->validate($post,'userType.add');
                if(true !== $validate){
                    throw new \LogicException($validate,1000);
                }
                if($group_arr['group_id'] == Models::GROUP_BUSINESS){
                    $post['bid'] = $group_arr['id'];
                }
                $bool = $type->_add($post);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        if($group_arr['group_id'] == Models::GROUP_BUSINESS){
            $flag = true;
        }
        // 商家列表
        $admin = new Models('admin');
        $business  = $admin->joinMany([['business b','a.id = b.id','right']],['a.status'=>Models::STATUS_ON,'b.id'=>['<>',Models::OFFICIAL]],'a.id,b.name');
        return view('typeAdd',['business'=>$business,'flag'=>$flag]);
    }

    /**
     * 商品类型修改
     * @return array|\think\response\View
     */
    public function typeSave(){
        // 判断商家登录
        $group_arr = self::getGroup();
        $type = new Models('goodsType');
        if(request()->isAjax()){
            $post = input('param.');
            try{
                // 修改场景验证
                $validate = $this->validate($post,'userType.save');
                if(true !== $validate){
                    throw new \LogicException($validate,1000);
                }
                if($group_arr['group_id'] == Models::GROUP_BUSINESS){
                    $post['bid'] = $group_arr['id'];
                }
                $bool = $type->_save($post);
                if(false === $bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
        $get  = input('param.');
        $info = $type->_find($get['id']);
        if($group_arr['group_id'] == Models::GROUP_BUSINESS){
            $flag = true;
        }
        // 商家列表
        $admin = new Models('admin');
        $business  = $admin->joinMany([['business b','a.id = b.id','right']],['a.status'=>Models::STATUS_ON,'b.id'=>['<>',Models::OFFICIAL]],'a.id,b.name');
        return view('typeSave',['info'=>$info,'business'=>$business,'flag'=>$flag]);
    }

    /**
     * 启用
     * @return array
     */
    public function typeStart(){
        if(request()->isAjax()){
            $post = input('param.');
            $type = new Models('goodsType');
            try{
                $bool = $type->setValue(['id'=>$post['id']],['status'=>$type::STATUS_ON]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }

    /**
     * 停用
     * @return array
     */
    public function typeStop(){
        if(request()->isAjax()){
            $post = input('param.');
            $type = new Models('goodsType');
            try{
                $bool = $type->setValue(['id'=>$post['id']],['status'=>$type::STATUS_OFF]);
                if(!$bool){
                    throw new \LogicException('操作失败',1010);
                }
            } catch (\Exception $e){
                return self::formatResult($e->getCode(),$e->getMessage());
            }
            return self::formatSuccessResult();
        }
    }
}